package com.baibei.ebec.feedback;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IUserApi;
import com.baibei.model.BalanceInfo;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.RxObservable;
import com.baibei.ui.AppUI;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Action;

/**
 * 意见反馈
 * Created by ChenRui on 2017/8/16 0016 19:42.
 */
@Route(path = AppRouter.ROUTER_FEEDBACK)
public class FeedbackActivity extends BasicActivity {

    @BindView(R2.id.et_text)
    EditText mText;
    @BindView(R2.id.tv_text_length)
    TextView mTextLength;
    @BindView(R2.id.btn_done)
    Button mDone;
    private IUserApi mUserApi;
    private String mTag = "FeedbackActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        mUserApi = ApiFactory.getInstance().getUserApi();

        mText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                int length = charSequence.length();
                mTextLength.setText(length + "/200");
                mDone.setEnabled(length > 3 && length <= 200);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public IPresenter getBasicPresenter() {
        return null;
    }

    @OnClick(R2.id.btn_done)
    public void onDoneClick() {

        mDone.setEnabled(false);
        mDone.setText("提交中，请稍后..");
        RxObservable.create(mUserApi.getBalanceInfo(), mTag)
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        mDone.setEnabled(true);
                        mDone.setText("提交");
                    }
                })
                .subscribe(new ApiDefaultObserver<BalanceInfo>() {
                    @Override
                    protected void onError(String s) {
                        AppUI.failed(getContext(), s);
                    }

                    @Override
                    protected void accept(BalanceInfo balanceInfo) {
                        AppUI.success(getContext(), "感谢反馈");
                    }
                });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxObservable.dispose(mTag);
    }
}
